//
//  FKLoginTool.m
//  FKLibraryExample
//
//  Created by frank on 2018/8/16.
//  Copyright © 2018年 zmosa - frank. All rights reserved.
//

#import "FKLoginTool.h"
#import "LoginViewController.h"
#import "FKNavVC.h"
#import "FKTabBarVC.h"
#import "JPAudioMainVC.h"

@implementation FKLoginTool

+ (void)showLoginByPresent
{
    LoginViewController *login = [[LoginViewController alloc] init];
    FKNavVC *nav = [[FKNavVC alloc] initWithRootViewController:login];
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    if ([window.rootViewController.presentedViewController isKindOfClass:[JPAudioMainVC class]]) {
        [window.rootViewController.presentedViewController fk_presentViewController:nav animated:YES completion:nil];
    } else {
        [window.rootViewController fk_presentViewController:nav animated:YES completion:nil];
    }
}

+ (void)showLoginBySwitchRootVC
{
    LoginViewController *login = [[LoginViewController alloc] init];
    FKNavVC *nav = [[FKNavVC alloc] initWithRootViewController:login];
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    window.rootViewController = nav;
}

+ (void)checkLogin:(void (^)(void))completion
{
    if ([FKCacheTool isLogin]) {
        // 还需要调用用户信息的接口来判断token是否失效【因为从其他端登录的话会导致token失效】
        [FKHttpTool postWithSuffix:@"/api/user/index" param:nil success:^(FKBaseResult *result) {
            if (result.isSuccess) {
                if (completion) completion();
            } else {
                [self showLoginByPresent];
            }
        } failure:^(NSError *error) {
            if ([error.localizedDescription containsString:@"401"]) {
                [SVProgressHUD showInfoWithStatus:@"请登录后操作!"];
            }
        }];
    } else {
        [self showLoginByPresent];
    }
}

+ (void)changeRootVC
{
//    UIWindow *window = [UIApplication sharedApplication].delegate.window;
//    window.rootViewController = [[FKTabBarVC alloc] init];
}


+ (void)checkAppVersion
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"version"] = [UIApplication sharedApplication].fk_appVersion;
    [FKHttpTool getWithSuffix:@"/api/index/Version" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            // 1当前版本大于系统版本， 2当前版本等于系统版本， 3当前版本小于系统版本
            NSString *status = result.data[@"status"];
            
            NSDictionary *data = result.data[@"data"];
            NSString *newversion = data[@"newversion"];
            BOOL enforce = [data[@"enforce"] boolValue];
            
            if ([status isEqualToString:@"3"]) {
                UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController;
                [vc fk_alertWithTitle:@"更新" message:@"有新的版本需要更新" cancelTitle:enforce ? nil : @"取消" confirmTitle:@"去更新" cancel:nil confirm:^{
                    [FKCacheTool saveOnlineVersion:newversion];
                    [FKCacheTool saveClassInfo:nil]; // 移除班级信息
                    [GC_OCTool clearAllFilePathAction];
                    [[NSNotificationCenter defaultCenter]postNotificationName:@"contentDataUpdate" object:nil];
                }];
            }
        }
    } failure:nil];
}


+ (void)checkContentVersion
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
//    param[@"version"] = [FKCacheTool contentVersion] ? : [UIApplication sharedApplication].fk_appVersion;
//    param[@"version"] = @"1684377746";
//    param[@"version"] = [FKCacheTool contentVersion] ? : fkFORMAT(@"%.0f", [[NSDate date] timeIntervalSince1970]);
    param[@"version"] = [UIApplication sharedApplication].fk_appVersion;
    [FKHttpTool getWithSuffix:@"/api/index/contentVersion" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            NSDictionary *data = result.data;
            
            if ([data isKindOfClass:[NSDictionary class]]) {
                // 每次启动，你拿到app时间戳和服务器上的对比, 如果没有修改/编辑/添加，那么时间戳一直是之前的时间
                NSString *newversion = data[@"newversion"];
                NSString *localContentVersion = [FKCacheTool contentVersion];
                if (localContentVersion) { // 有缓存的contentVersion
                    NSDate *oldTime = [NSDate dateWithTimeIntervalSince1970:localContentVersion.doubleValue];
                    NSDate *newTime = [NSDate dateWithTimeIntervalSince1970:newversion.doubleValue];
                    if ([newTime compare:oldTime] == NSOrderedDescending) { // 新版大于旧版【代表有更新】
                        [self updateContentVersion:newversion];
                    }
                } else { // 没有缓存【代表首次请求】
                    [self updateContentVersion:newversion];
                }
            }
        }
    } failure:nil];
}

+ (void)updateContentVersion:(NSString *)contentVersion
{
    [FKCacheTool saveContentVersion:contentVersion];
    [FKCacheTool saveClassInfo:nil]; // 移除班级信息
    [FKCacheTool clearRequestData];
    [GC_OCTool clearAllFilePathAction]; // 清除所有缓存的接口数据文档内容
    [[NSNotificationCenter defaultCenter]postNotificationName:@"contentDataUpdate" object:nil];
}

@end
